using System;
using System.Linq;
using UMC.Data;
using UMC.Web;
using UMC.WorkFlow.Entities;

namespace UMC.WorkFlow
{


    [Apiumc("WorkFlow", "Setting", Auth = WebAuthType.Admin, Desc = "工作流设置")]
    public class DesignSettingActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var Task = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Task", "none")).Value;

            var design = DataFactory.Instance().TaskDesign(Task);
            var form = DataFactory.Instance().TaskForm(design.TaskId.Value);


            var Type = this.AsyncDialog("Type", g =>
            {
                var selt = new UISelectDialog() { Title = "操作" };
                selt.Options.Add("流程版本", "Design"); ;
                selt.Options.Add("流程标题", "Text");
                selt.Options.Add("表单设置", "Setting");
                selt.Options.Add("更新实例", "Publish");
                if (design.Id != form.DesignId)
                {
                    selt.Options.Add("发布流程", "OK");
                }
                return selt;
            });
            var instance = UMC.Data.JSON.Deserialize<TaskInstance>(TaskManager.Design(Task));
            switch (Type)
            {
                case "Publish":
                    response.Redirect(request.Model, "Publish", Task.ToString());
                    break;
                case "Form":
                    response.Redirect(request.Model, $"Form.{form.Code}");
                    break;
                case "OK":
                    DataFactory.Instance().Put(new TaskForm { TaskId = form.TaskId, DesignId = design.Id });
                    this.Prompt("发布成功");
                    break;
                case "Design":
                    var Design = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Design", g =>
                    {
                        var gi = new TaskDesignGridDialog() { Title = "流程版本", TaskId = form.TaskId, CloseEvent = "WorkFlow.Design" };
                        gi.Menu("新建版本", request.Model, request.Command, new WebMeta().Put("Task", Task.ToString(), "Type", "News"));
                        return gi;
                    })).Value;

                    this.Context.Send("WorkFlow.Design", new WebMeta().Put("Task", UMC.Data.JSON.Expression(TaskManager.Design(Design))), true);
                    break;
                case "Setting":
                    response.Redirect(request.Model, "Form", design.TaskId.ToString());
                    break;
                case "News":
                    var version = DataFactory.Instance().TaskDesigns(form.TaskId.Value).MaxBy(v => v.Version ?? 0)?.Version ?? 0; ;
                    // var version = entity.Where.Reset().And().Equal(new TaskDesign
                    // {
                    //     TaskId = form.TaskId
                    // }).Entities.Max(new TaskDesign
                    // {
                    //     Version = 0
                    // }).Version ?? 0;
                    version++;

                    instance.Text = String.Format("{0} v{1}.{2}", form.Caption, (version / 10) + 1, (version) % 10);
                    instance.InstanceId = Utility.NewId();
                    instance.TaskId = form.TaskId.Value;
                    var body = UMC.Data.JSON.Serialize(instance, true);
                    DataFactory.Instance().Put(new TaskDesign
                    {
                        Id = instance.InstanceId,
                        Name = instance.Text,
                        TaskId = form.TaskId,
                        Context = body,
                        Version = version,
                        UpdateDate = DateTime.Now,
                        Username = this.Context.Token.Username
                    });
                    this.Context.Send("WorkFlow.Design", new WebMeta().Put("Task", UMC.Data.JSON.Expression(body)), true);
                    break;
                default:
                    var Text = this.AsyncDialog("Text", g =>
                    {
                        return new UITextDialog() { Title = "流程标题", DefaultValue = form.Caption };
                    });
                    DataFactory.Instance().Put(new TaskForm { Caption = Text ,TaskId=form.TaskId });
                    instance.Text = String.Format("{0} v{1}.{2}", Text, ((design.Version ?? 0) / 10) + 1, (design.Version ?? 0) % 10);

                    TaskManager.Design(instance, this.Context.Token.Username);

                    var json = UMC.Data.JSON.Serialize(instance, true);
                    this.Context.Send("WorkFlow.Design", new WebMeta().Put("Task", UMC.Data.JSON.Expression(json)), true);

                    break;
            }


        }
    }
}